package com.kingyea.app.market.utils;

import org.springframework.web.context.request.RequestContextHolder;
import org.springframework.web.context.request.ServletRequestAttributes;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;


/**
 * @author Mr.Lin
 * @version v1.0.0
 * @description 上下文 ( 但是暴露了servlet )
 * @date Created in 2017/12/31 14:07
 */
public class ContextHolderUtils {

    /**
     * request
     *
     * @return HttpServletRequest
     * @author Mr.Lin
     * @date 2017/12/31 14:11
     */
    public static HttpServletRequest getRequest() {
        if (RequestContextHolder.getRequestAttributes() == null) {
            return null;
        } else {
            return ((ServletRequestAttributes) RequestContextHolder.getRequestAttributes()).getRequest();
        }
    }

    /**
     * session
     *
     * @return HttpSession
     * @author Mr.Lin
     * @date 2017/12/31 14:12
     */
    public static HttpSession getSession() {
        if (getRequest() == null) {
            return null;
        } else {
            return getRequest().getSession();
        }
    }

    /**
     * token
     *
     * @return Object
     * @author Mr.Lin
     * @date 2017/12/29 14:12
     */
    public static Object getToken() {
        if (getRequest() == null) {
            return null;
        } else {
            return getRequest().getHeader(GlobalVar.TOKENNAME);
        }
    }


}
